<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN">
<?php
	if (!isset($_SESSION)) {
		session_start();
		if(!isset($_SESSION["userid"])) {
			// jgn lupa di redirect ke mana
			header('Location: index.php');
		}
	}
	require("php/akses.php");
	require("php/Profile.php");
	require("php/validation.php");
	
	if (!empty($_POST)) {
		$content=array();
		$content['judul'] = (isset($_POST['text_judul'])) ? trim($_POST['text_judul']) : null;
		$content['tipe'] = (isset($_POST['radio_tipe'])) ? $_POST['radio_tipe'] : null;
		$content['URL'] = (isset($_POST['text_path'])) ? trim($_POST['text_path']) : null;
		if ($content['tipe'] == 'Gambar') {
			$content['URL'] = (isset($_FILES["text_path"]["name"])) ? trim($_FILES["text_path"]["name"]) : null;
		}
		$tag = (isset($_POST['text_tag'])) ? trim($_POST['text_tag']) : null;
		$content['tags'] = setTagArray($tag);
		$content['deskripsi'] = (isset($_POST['text_desc'])) ? trim($_POST['text_desc']) : null;
		$pesan = isContentValid($content);
		if (count($pesan)==0) {
			if (isLogin()) {
				connect();
				if ($content['tipe']=='Video') {
					$content['URL'] = (isset($_POST['text_path2'])) ? trim($_POST['text_path2']) : null;
				} else if ($content['tipe']=='Gambar') {
					//$content['URL'] = savePostImage($_SESSION['username']);
					if ($_FILES["text_path"]["error"] > 0) {
						$content['URL'] = "Return Code: " . $_FILES["text_path"]["error"] . "<br />";
					} else {
						$uname = $_SESSION['username'];
						$avatarfolder = "images/post/";
						$savefilename = $uname.".jpg";
						$i = 1;
						while (file_exists($avatarfolder . $savefilename)) {
							$savefilename = $uname."(".$i.").jpg";
							$i++;
						}
						move_uploaded_file($_FILES["text_path"]["tmp_name"],
						$avatarfolder . $savefilename);
						$content['URL'] = $avatarfolder . $savefilename;
					}
				}
				
				addContent(getLoginUserid(), $content);
				// jgn lupa achievement
				$newach = checkAchievementPost(getLoginUserid());
				foreach ($newach as $ach) {
					addAchievement($getLoginUserid(), $ach['ID_achievement']);
				}
				// jgn lupa ke halaman konten yg baik dan benar
				header('Location: konten.php');
			} else {
				header('Location: index.php');
			}
		}
	}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<link rel="stylesheet" type="text/css" href="css/style.css" />
	<script type="text/javascript" src="javascript/foodholic.js"></script>
	<title>Post</title>
</head>

<body>
	
	<?php require "header.php"; ?>
		
		<div id="content">
			<h2 id="content_head">POST NEW CONTENT</h2>
			<div id="post_preview"></div>
				<div id="fields">
					<div class="input_label">Judul</div>
					<div class="input_label">Tipe</div>
					<div class="input_label">
						<?php
							if (isset($content['tipe'])) {
								switch ($content['tipe']) {
									case "Link":
										echo "URL";
										break;
									case "Video":
										echo "URL YouTube";
										break;
									case "Gambar":
										echo "Path File";
										break;
								}
							}
						?>
					</div>
					<div class="input_label"><?php if (isset($content['tipe']) && ($content['tipe'] == 'Link')) echo "Deskripsi"; ?></div>
					<div class="input_label">Tag</div>
				</div>
			<form action="post.php" enctype="multipart/form-data" method="post">
				<div id="inputs">
					<div class="input_box"><input type="text" name="text_judul" size="30" value="<?php if (isset($content['judul'])) echo $content['judul']; ?>" /></div>
					<div class="input_box">
						<input type="radio" name="radio_tipe" value="Link" onchange="radio_tipe_change()" <?php if (isset($content['tipe']) && ($content['tipe'] == 'Link')) echo 'checked="checked"'; ?> />Link
						<input type="radio" name="radio_tipe" value="Gambar" onchange="radio_tipe_change()" <?php if (isset($content['tipe']) && ($content['tipe'] == 'Gambar')) echo 'checked="checked"'; ?>/>Image
						<input type="radio" name="radio_tipe" value="Video" onchange="radio_tipe_change()" <?php if (isset($content['tipe']) && ($content['tipe'] == 'Video')) echo 'checked="checked"'; ?> />Video
					</div>
					<div class="input_box" id="input_path">
						<?php 
							if (isset($content['tipe'])) {
								if (($content['tipe'] == 'Link') || ($content['tipe'] == 'Video')) {
									echo '<input type="text" name="text_path" onKeyUp="cekYoutube();" onChange="cekYoutube();" onBlur="cekYoutube();" size="40"';
									if (isset($content['URL']))
										echo ' value="'.$content['URL'].'"';
								} else if ($content['tipe'] == 'Gambar') {
									echo '<input type="file" name="text_path"';
									if (isset($content['URL']))
										echo " value=\"".$content['URL'].'"';
								}
								echo ' />';
							}
						?>
					</div>
					<div class="input_box" id="input_desc">
						<?php 
							if (isset($content['tipe']) && ($content['tipe'] == 'Link')) {
								echo '<input type="text" name="text_desc" size="40"';
								if (isset($content['deskripsi'])) {
									echo " value=\"".$content['deskripsi'].'"';
								}
								echo ' />';
							}
						?>
					</div>
					<div class="input_box" id="input_tag">
						<?php 
								echo '<input type="text" name="text_tag" size="40"';
								if (isset($_POST['text_tag'])) {
									echo " value=\"".$_POST['text_tag'].'"';
								}
								echo ' />';
						?>
					</div>
						<input type="hidden" name="text_path2" size="40" value="<?php if (isset($content['URL'])) echo $content['URL']; ?>" />
				</div>
				<input type="button" name="btn_kirim" value="Preview" onClick="PreviewPost();" />
				<input type="submit" name="btn_kirim" value="Kirim!" />
				<input type="button" name="btn_kirim" value="Cancel" onClick="CancelPost();" />
				<script type="text/javascript">
					document.getElementsByName("btn_kirim")[2].style.visibility = "hidden";
				</script>
			</form>
				<div id="errors">
						<div class="input_label"><?php if (isset($pesan['judul'])) echo $pesan['judul']; ?></div>
						<div class="input_label"><?php if (isset($pesan['tipe'])) echo $pesan['tipe']; ?></div>
						<div class="input_label">
							<?php
								if (isset($content['tipe'])) {
									switch ($content['tipe']) {
										case "Link":
											if (isset($pesan['URL'])) echo $pesan['URL'];
											break;
										case "Video":
											if (isset($pesan['URL'])) echo $pesan['URL'];
											break;
										case "Gambar":
											if (isset($pesan['URL'])) echo $pesan['URL'];
											break;
									}
								}
							?>
						</div>
						<div class="input_label"><?php if (isset($pesan['deskripsi'])) echo $pesan['deskripsi']; ?></div>
						<div class="input_label"><?php if (isset($pesan['tags'])) echo $pesan['tags']; ?></div>
					</div>
		</div>
		
		<?php require "footer.php"; ?>
		
		
	</div>
</body>
</html>